<template>
  <div class="register-container">
    <el-card class="register-card">
      <h2>注册</h2>
      <el-form :model="form" ref="formRef" label-width="100px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="form.password" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleRegister">注册</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { useUserStore } from '@/stores/userStore.js'; // 确保路径正确
import { ElMessage } from 'element-plus';

const router = useRouter();
const store = useUserStore();

const form = ref({
  username: '',
  password: ''
});

const handleRegister = () => {
  const { username, password } = form.value;
  if (store.register(username, password)) {
    ElMessage.success('注册成功');
    router.push('/login');
  } else {
    ElMessage.error('用户名已存在');
  }
};
</script>

<style scoped>
/* 你的样式代码 */
</style>
